﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;
namespace LiWi
{
    public partial class 凸轮跟随 : Form
    {
        public WindowsAdapt adapt = new WindowsAdapt();
        IntPtr _handle { get { return Data.handle; } }
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }
        public 凸轮跟随()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            string[] datas = new string[2];
            int r = 100;
            int d = 2 * r;
            double x = 0;
            for (int i = 0; i <= 360; i++)
            {
                datas[0]=Convert.ToString(i+1);


                //计算一个凸轮表
                datas[1] = Convert.ToString(i * 25 + 10000 * (1 - Math.Cos((i * Math.PI / 180))));           
                DataGridView_posList.Rows.Add(datas);
             
            }
            adapt.SetForm(this);
        }

        private async void Button1_Click(object sender, EventArgs e)
        {
            
            if (isConnect)
            {
                PMC_Rapidstop(_handle, 3);
                Button1.Enabled = false;

                float dis = (float)numericUpDown_dis.Value;
                float spd = (float)numericUpDown_spd.Value;
                int mainAxis = (int)numericUpDown_mainAxis.Value;
                int camAxis = (int)numericUpDown_connectAxis.Value;
                float camAxisUnits = 0;

                float[] value = new float[1];
                for (int i = 0; i < DataGridView_posList.Rows.Count; i++) {
                    value[0] =Convert.ToSingle(DataGridView_posList.Rows[i].Cells[1].Value);
                    PMC_SetTable(_handle, i, 1, value);
                }

                await Task.Run(async ()=>
                {
                    PMC_SetAtype(_handle, mainAxis, 1); PMC_SetAtype(_handle, camAxis, 1);
                    PMC_SetUnits(_handle, mainAxis, 100); PMC_SetUnits(_handle, camAxis, 100);
                    PMC_SetSpeed(_handle, mainAxis, spd); PMC_SetSpeed(_handle, camAxis, spd);
                    Thread.Sleep(10);
                    PMC_GetUnits(_handle, camAxis, ref camAxisUnits);
                    //启动凸轮链接，凸轮表放在table0-360中，主轴走dis变量的距离跟随轴就运行一遍table0-360中的凸轮数据
                    PMC_Cambox(_handle, camAxis, 0, 360, camAxisUnits, dis, mainAxis, 6, 0);
                    Thread.Sleep(100);
                    //判断缓冲剩余量:如果缓冲小于20等待一下
                    await Task.Run(() =>
                    {
                        int[] axisList = { camAxis, mainAxis };
                        while (Data.ReMainBuffer(axisList) <= 20)
                        {
                            Thread.Sleep(200);
                        }
                        PMC_Single_Move(_handle, mainAxis, dis);
                    });
                });
                Button1.Enabled = true;
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            if (isConnect) {
                PMC_Rapidstop(_handle, 2);
            }
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isConnect) {
                float pos1=0, pos2=0;
                int mainAxis = (int)numericUpDown_mainAxis.Value;
                int camAxis = (int)numericUpDown_connectAxis.Value;
                PMC_GetDpos(_handle, mainAxis, ref pos1);
                PMC_GetDpos(_handle, camAxis, ref pos2);

                label_main.Text = pos1.ToString();
                labelCam.Text = pos2.ToString();
            }
        }
    }
}
